#pragma once
#pragma warning(disable:4996)       // disable checked iterator errors http://msdn.microsoft.com/en-us/library/aa985965(VS.80).aspx 

//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101

#include <Macros.h>
#include <CoVector.h>
#include <CoMatrix.h>
#include <CoCube.h>
#include <ValueHelpers.h>
#include <Settings.h>

#pragma unmanaged 
#include <ql\math\array.hpp>
#include <boost/smart_ptr/detail/spinlock.hpp>
#pragma managed 

using namespace System;
using namespace QuantLib;
using namespace Cephei;

#undef HANDLE
#undef ABSTRACT
#define STRUCT
namespace Cephei { namespace QL { namespace Math {
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IArray
	public ref class CArray : 
            public System::MarshalByRefObject,
            public Cephei::QL::Math::IArray
	{
	protected: 
		boost::shared_ptr<QuantLib::Array>* _ppArray;
        boost::detail::spinlock* _pSpinlock;
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Array>* _phArray;
#endif
		Object^ _ArrayOwner;     // reference to object that manages the storage for this object
	internal:
		CArray (Cephei::QL::Math::IArray^ prm1);
		CArray (UInt64 size, Double value, Double increment);
		CArray (UInt64 size, Double value);
		CArray (Microsoft::FSharp::Core::FSharpOption<UInt64>^ size);
        CArray (boost::shared_ptr<QuantLib::Array>& childNative, Object^ owner);
        CArray (QuantLib::Array& childNative, Object^ owner);
        CArray (CArray^ copy);
        CArray (System::Type^ t);
#ifdef STRUCT
        CArray (QuantLib::Array childNative);
#endif       
#ifdef HANDLE
		CArray (QuantLib::Handle<QuantLib::Array>& childNative, Object^ owner);
		CArray (QuantLib::Handle<QuantLib::Array> childNative);
#endif
		virtual ~CArray ();
		!CArray ();

	internal:
		QuantLib::Array& GetReference ();
		boost::shared_ptr<QuantLib::Array>& GetShared ();
		QuantLib::Array* GetPointer ();
        void SetArray (boost::shared_ptr<QuantLib::Array> native)
        {
            if (_ppArray != NULL)
                delete _ppArray;
            _ppArray = new boost::shared_ptr<QuantLib::Array> (native);
            
        }
#ifdef HANDLE
		QuantLib::Handle<QuantLib::Array>& GetHandle ();
#endif
		virtual bool HasNative () ;
		void Lock ()
		{
			if (Cephei::QL::Settings::_ReferenceLocking)
    			_pSpinlock->lock ();
		}
		void Unlock ()
		{
			_pSpinlock->unlock ();
		}
    public:
		virtual Double At (UInt64 prm1) ;
        property Double Back 
        {
		    virtual Double get () ;
        }
        property Double Begin 
        {
		    virtual Double get () ;
        }
        property Boolean Empty 
        {
		    virtual Boolean get () ;
        }
        property Double End 
        {
		    virtual Double get () ;
        }
        property Double Front 
        {
		    virtual Double get () ;
        }
        property UInt64 Size 
        {
		    virtual UInt64 get () ;
        }
		virtual Cephei::QL::Math::IArray^ Swap (Cephei::QL::Math::IArray^ prm1) ;
    };
	//////////////////////////////////////////////////////////////////////////////////////////////
	// implementation of IArray Vector interface
	ref class CArrayVector : public System::MarshalByRefObject,  
							    public INativeVector<IArray^>,
							    public INativeVector<Generic::ICell<IArray^>^>
	{
	private:
#ifdef STRUCT
		std::vector<QuantLib::Array>* _pVValue;
#endif        
		std::vector<boost::shared_ptr<QuantLib::Array>>* _pVBoost;
#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Array>>* _pVHand;
#endif
		Object^ _owner;		// object reference to prevent GC of vector owner

	internal:
		CArrayVector ();
#ifdef STRUCT
		CArrayVector (std::vector<QuantLib::Array>& vec, Object^ owner);
		CArrayVector (std::vector<QuantLib::Array> vec);
#endif       
		CArrayVector (std::vector<boost::shared_ptr<QuantLib::Array>>& vec, Object^ owner);
#ifdef HANDLE
		CArrayVector (std::vector<QuantLib::Handle<QuantLib::Array>>& vec, Object^ owner);
#endif
		virtual ~CArrayVector ();
		!CArrayVector ();

    public:
		property int Count 
		{
			virtual int get ();
		}

		property IArray^ default[int] 
		{
			virtual IArray^ get (int index) = INativeVector<IArray^>::default::get;
			virtual void set (int index, IArray^ value) = INativeVector<IArray^>::default::set;
		}

		//////////////////////////////////////////////////////////////////////////////////////////
		// Interface functions for cell vectors 
	public:
		property Generic::ICell<Cephei::QL::Math::IArray^>^ CItem[int] 
		{
			virtual Generic::ICell<IArray^>^ get (int index) = INativeVector<Generic::ICell<IArray^>^>::default::get;
			virtual void set (int index, Generic::ICell<IArray^>^ value) = INativeVector<Generic::ICell<IArray^>^>::default::set;
		}
	internal:
#ifdef STRUCT    
		std::vector<QuantLib::Array>& GetReference ();
#endif     
		std::vector<boost::shared_ptr<QuantLib::Array>>& GetShared ();

#ifdef HANDLE
		std::vector<QuantLib::Handle<QuantLib::Array>>& GetHandle ();
#endif
	public:
		virtual bool PrepareFeature (NativeFeature feature);
	};
    
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Factory class
	public ref class CArray_Factory : public System::MarshalByRefObject,  public IArray_Factory
	{
	public:
        virtual IArray^ Create (Cephei::QL::Math::IArray^ prm1);
        virtual IArray^ Create (UInt64 size, Double value, Double increment);
        virtual IArray^ Create (UInt64 size, Double value);
        virtual IArray^ Create (Microsoft::FSharp::Core::FSharpOption<UInt64>^ size);
        /* <summary> Create a hybrid Vector of IArray, with event notification of changes </summary> */
        virtual Cephei::IVector<IArray^>^ CreateVector();
        /* <summary> Create a hybrid Vector of ICell of IArray, with event notification of changes </summary> */
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IArray^>^>^>^ CreateCellVector();
        virtual Cephei::IVector<IArray^>^ CreateVector(System::Collections::Generic::IEnumerable<IArray^>^ source);
        virtual Generic::ICell<Cephei::IVector<Generic::ICell<IArray^>^>^>^ CreateCellVector(System::Collections::Generic::IEnumerable<Generic::ICell<IArray^>^>^ source);
    };
   
/*Cephei*/ } /*QL*/ } /*Math */}
